package cn.tedu.article.controller;

import cn.tedu.article.pojo.param.*;
import cn.tedu.article.pojo.vo.ArticleVO;
import cn.tedu.article.pojo.vo.CategoryVO;
import cn.tedu.article.pojo.vo.TagVO;
import cn.tedu.article.service.ArticleService;
import cn.tedu.base.response.JsonResult;
import cn.tedu.base.response.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/article")
@Api(tags = "文章模块")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    @PostMapping("/save")
    @ApiOperation("保存文章")
    private JsonResult saveArticle(@RequestBody ArticleSaveParam articleSaveParam){
        log.debug("文章保存:{}",articleSaveParam);
        ArticleVO articleVO = articleService.saveArticle(articleSaveParam);
        log.debug("文章保存,成功:{}",articleSaveParam);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,articleVO);
    }

    @PostMapping("/delete/{articleId}")
    @ApiOperation("删除文章")
    public JsonResult deleteArticle(@PathVariable Long articleId){
        log.debug("删除文章: articleId:{}",articleId);
        articleService.deleteArticle(articleId);
        log.debug("删除文章,成功: articleId:{}",articleId);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }

    @GetMapping("/select/{articleId}/{userId}")
    @ApiOperation("通过文章id获取文章")
    public JsonResult selectArticleById(@PathVariable Long articleId,@PathVariable Long userId){
        log.debug("通过文章id获取文章:{},用户id:{}",articleId,userId);
        ArticleVO articleVO = articleService.selectArticleById(articleId,userId);
        log.debug("通过文章id获取文章,成功: articleId:{}",articleId);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,articleVO);
    }

    @GetMapping("/list/{userId}")
    @ApiOperation("通过用户id获取文章")
    public JsonResult selectArticleByUserId(@PathVariable Long userId){
        log.debug("通过用户id获取文章:{}",userId);
        List<ArticleVO> articleVOList = articleService.selectArticleByUserId(userId);
        log.debug("通过用户id获取文章,成功: articleId:{}",userId);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,articleVOList);
    }

    @PostMapping("/list")
    @ApiOperation("查询文章")
    public JsonResult selectArticleByArticleQueryParam(@RequestBody ArticleQueryParam articleQueryParam){
        log.debug("通过文章查询对象查询文章:{}",articleQueryParam);
        List<ArticleVO> articleVOList = articleService.selectArticleByArticleQueryParam(articleQueryParam);
        log.debug("通过用户id获取文章,成功: articleQueryParam:{}",articleQueryParam);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,articleVOList);
    }

    @PostMapping("/list/tags")
    @ApiOperation("查询标签")
    public JsonResult selectArticleTags(@RequestBody ArticleTagQueryParam articleTagQueryParam){
        log.debug("获取文章标签:{}",articleTagQueryParam);
        List<TagVO> tagVOList = articleService.selectArticleTags(articleTagQueryParam);
        log.debug("获取文章标签成功:{}",tagVOList);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,tagVOList);
    }

    @PostMapping("/list/category")
    @ApiOperation("查询分类")
    public JsonResult selectArticleCategory(@RequestBody ArticleCategoryQueryParam articleCategoryQueryParam){
        log.debug("获取文章分类:{}",articleCategoryQueryParam);
        List<CategoryVO> categoryVOList = articleService.selectArticleCategory(articleCategoryQueryParam);
        log.debug("获取文章分类成功:{}",categoryVOList);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,categoryVOList);
    }

    @PostMapping("/onlike/{articleId}/{userId}")
    @ApiOperation("文章点赞")
    public JsonResult articleOnLike(@PathVariable Integer articleId, @PathVariable Integer userId){
        log.debug("文章点赞,articleId:{},userId:{}",articleId,userId);
        articleService.articleOnLike(articleId,userId);
        log.debug("文章点赞,成功,articleId:{},userId:{}",articleId,userId);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }

    @PostMapping("/cancelLike/{articleId}/{userId}")
    @ApiOperation("取消文章点赞")
    public JsonResult articleCancelLike(@PathVariable Integer articleId, @PathVariable Integer userId){
        log.debug("取消文章点赞,articleId:{},userId:{}",articleId,userId);
        articleService.articleCancelLike(articleId,userId);
        log.debug("取消文章点赞,成功,articleId:{},userId:{}",articleId,userId);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }


    @PostMapping("/onfavorite/{articleId}/{userId}")
    @ApiOperation("文章收藏")
    public JsonResult articleOnFavorite(@PathVariable Integer articleId, @PathVariable Integer userId){
        log.debug("文章收藏,articleId:{},userId:{}",articleId,userId);
        articleService.articleOnFavorite(articleId,userId);
        log.debug("文章收藏,成功:,articleId:{},userId:{}",articleId,userId);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }

    @PostMapping("/cancelFavorite/{articleId}/{userId}")
    @ApiOperation("取消文章收藏")
    public JsonResult articleCancelFavorite(@PathVariable Integer articleId, @PathVariable Integer userId){
        log.debug("取消文章收藏,articleId:{},userId:{}",articleId,userId);
        articleService.articleCancelFavorite(articleId,userId);
        log.debug("取消文章收藏,成功,articleId:{},userId:{}",articleId,userId);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }

    @PostMapping("/visit/{articleId}")
    @ApiOperation("文章浏览记录")
    public JsonResult articleVisit(@PathVariable Integer articleId){
        log.debug("文章浏览记录:{}",articleId);
        articleService.articleVisit(articleId);
        log.debug("文章浏览记录,成功:{}",articleId);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);

    }

    @PostMapping("ai")
    @ApiOperation("AI功能")
    public ResponseBodyEmitter promptAI (@RequestBody AiRecordSaveParam aiRecordSaveParam){

        /*String s = articleService.visitAI(userInput);
        return s;*/
        ResponseBodyEmitter emitter =new ResponseBodyEmitter();

        //异步调用服务
        new Thread(()-> {
            try {
                articleService.visitAI(emitter,aiRecordSaveParam);
            } catch (Exception e) {
                emitter.completeWithError(e);
            }
        }).start();

        return emitter;
    }
}
